/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package equipment;

import address.*;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.List;

/**
 *
 * @author PC
 */
public class EquipmentAction extends ActionSupport implements ModelDriven<Equipment> {

    private static final long serialVersionUID = 9149826260758390091L;
    private int id_equipment;
    private String name_hardware;
    private String cpu;
    private String ram;
    private String lieu;
    private String system_ex;   
    private EquipmentDAO equipmentDao;
    private List<Equipment> listEquipement;
    private Addresses addresses;
    private AddressIpAction addressAction;
    private Equipment equipment;

    @Override
    public String execute() {
        System.out.println("-----method execute EquipmentAction");
        equipmentDao = new EquipmentDAO();
        
        equipment = new Equipment(id_equipment, name_hardware, cpu, ram, ram, system_ex);
        equipmentDao.addOrUpdate(equipment);
        
        addressAction.execute();
        
        return SUCCESS;
    }
    

    /*public String displayform() {
        addressIp = new AddressIp();
        return SUCCESS;
    }*/

    /*public String getListAddress() {
        addressDao = new EquipmentDAO();
        listAddressIp = addressDao.listAddress();
        return SUCCESS;
    }*/
    
    /**
     * Whether the form is properly completed
     * @return true or false
     */
    /*private boolean checkForm() {
        System.out.println("-----method checkForm");
        boolean is_correct = checkAddressOrNetmask();      
        if(name_hardware.isEmpty()) {
            addFieldError("name_hardware", getText("error.name_hardware"));
            is_correct = false;
        }       
        System.out.println("is_correct: " + is_correct);
        return is_correct;
    }*/
    
    /**
     * Checks if the input address is correct and the netmask if it is not empty
     * @return true or false 
     */
    /*private boolean checkAddressOrNetmask() {
        System.out.println("-----method checkAddressOrNetmask"); 
        boolean is_correct = true;
        try {
            if(!address.isEmpty()) {
               InetAddress.getByName(address); 
            }
            else {
                addFieldError("address", getText("error.addressEmpty"));
                is_correct = false;
            }          
        }
        catch (UnknownHostException e) {
            addFieldError("address", getText("error.address"));
            is_correct = false;
        }
        finally {
            if(!netmask.isEmpty()) {
                try {
                    InetAddress.getByName(netmask);     
                }
                catch (UnknownHostException e) {
                    addFieldError("netmask", getText("error.netmask"));
                    is_correct = false;
                }
                finally {
                    return is_correct;
                }
            }
            return is_correct;
        }
    }*/

    public String getName_hardware() {
        return name_hardware;
    }

    public void setName_hardware(String name_hardware) {
        this.name_hardware = name_hardware;
    }

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getRam() {
        return ram;
    }

    public void setRam(String ram) {
        this.ram = ram;
    }

    public String getLieu() {
        return lieu;
    }

    public void setLieu(String lieu) {
        this.lieu = lieu;
    }

    public String getSystem_ex() {
        return system_ex;
    }

    public void setSystem_ex(String system_ex) {
        this.system_ex = system_ex;
    }


    public List<Equipment> getListEquipement() {
        return listEquipement;
    }

    public void setListEquipement(List<Equipment> listEquipement) {
        this.listEquipement = listEquipement;
    }

    /*public String editAddressIp() {
        EquipmentDAO addressIpDao = new EquipmentDAO();
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        System.out.println("id: " + request.getParameter("id_address"));
        addressIp = addressIpDao.getAddressById(Integer.parseInt(request.getParameter("id_address")));
        return SUCCESS;
    }*/

    /*public String deleteAddressIp() {
        EquipmentDAO addressIpDao = new EquipmentDAO();
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        addressIpDao.deleteAddress(Integer.parseInt(request.getParameter("id_address")));
        listAddressIp = addressIpDao.listAddress();
        return SUCCESS;
    }*/

    @Override
    public Equipment getModel() {
        return equipment;
    }

    

    
}
